﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System;

namespace OestsServer.Handlers.UnityHandlers
{
    public class HandInExamHandler : HandlerBase<UnityClientChannel>
    {
        public override PacketKey PKey => PacketKey.HandInOperateExam;

        public override void Execute(UnityClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            //LIXINGJUN 不考虑错误题的问题
            ExamData data = JsonMapper.ToObject<ExamData>(resquestPacket.DataStr);
            data.Date = DateTime.Now.ToString();
            ResponsePacket = new Response(resquestPacket);
            //在成绩表中添加数据
            ResponsePacket.DataObject = data;
            if (StudentExamDAL.HandInExamGrade(data))//提交成功
            {
                //if (StudentExamDAL.UpdateExamCount(data.StudentId, data.ExamId))
                //    data.Count++;

                ResponsePacket.ReturnCode = ReturnCode.Success;
                Response response = new Response(PacketKey.BroadcastExamDataToTeacher, data);
                response.ReturnCode = ReturnCode.Success;

                OESPClientChannel.BrocastToTeacher.Dispatch(response);
            }
            else//提交失败
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "你的考试不存在，请确认你是否参加了该考试？";
            }
            client.Send(ResponsePacket);


            //在学生表中添加错题数据
            //         List<OperateError> dbList = StudentDAL.GetOperateErrorList(data.StudentId);
            //List<OperateError> unionList = data.OperateErrorList;
            //if (unionList != null)
            //{
            //	if (dbList != null)
            //	{
            //		unionList = unionList.Union(dbList, new OperateErrorComparer()).ToList<OperateError>();
            //	}
            //	string str = JsonMapper.ToJson(unionList);
            //	StudentDAL.UpdateOperateError(data.StudentId, str);
            //}
            //ResponsePacket.DataObject = data;
            //client.Send(ResponsePacket);
        }
    }
}
